﻿using MQ.Core.Common;
using MQ.Core.Db;
using MQ.Core.Entity;
using MQ.Core.IRepository;
using MQ.Core.Util;
using System;
using System.Collections.Generic;

namespace MQ.Core.Repository
{
    public class DictionaryTypeRepository : IDictionaryTypeRepository
    {
        private readonly ISqlDb db;

        public DictionaryTypeRepository(ISqlDb db)
        {
            this.db = db;
        }

        #region 查询数据
        public bool ExistByName(long parentId, string name)
        {
            string sql=$"select count(1) from sys_dictionarytype where parentid={parentId} and name='{name}'";
            return db.Exist(sql);
        }

        public bool ExistByValue(long parentId, string value)
        {
            string sql = $"select count(1) from sys_dictionarytype where parentid={parentId} and value='{value}'";
            return db.Exist(sql);
        }

        public IEnumerable<DictionaryTypeEntity> GetList(string query)
        {
            string sql = "select * from sys_dictionarytype where 1=1";
            var json = query.ToJObject();
            if (!json["id"].IsEmpty())
            {
                sql +=$" and id={json["id"].ToInt64()}";
            }
            if (!json["parentId"].IsEmpty())
            {
                sql += $" and parentId={json["parentId"].ToInt64()}";
            }
            if (!json["name"].IsEmpty())
            {
                sql += $" and name='{json["name"].ToString()}'";
            }
            if (!json["value"].IsEmpty())
            {
                sql += $" and value='{json["value"].ToString()}'";
            }
            return db.GetList<DictionaryTypeEntity>(sql);
        }

        public IEnumerable<DictionaryTypeEntity> GetPageList(Pagination pagination, string query)
        {
            string sql = "select * from sys_dictionarytype where 1=1";
            var json = query.ToJObject();
            if (!json["id"].IsEmpty())
            {
                sql += $" and id={json["id"].ToInt64()}";
            }
            if (!json["parentId"].IsEmpty())
            {
                sql += $" and parentId={json["parentId"].ToInt64()}";
            }
            if (!json["name"].IsEmpty())
            {
                sql += $" and name='{json["name"].ToString()}'";
            }
            if (!json["value"].IsEmpty())
            {
                sql += $" and value='{json["value"].ToString()}'";
            }
            return db.GetList<DictionaryTypeEntity>(sql, pagination);
        }
        #endregion

        #region 提交数据
        public int Add(DictionaryTypeEntity entity)
        {
            string sql = $"insert into Sys_DictionaryType(parentid,name,value,status,[order],remark) values({entity.ParentId},'{entity.Name}','{entity.Value}',{entity.Status},{entity.Order},'{entity.Remark}')";
            return db.Execute(sql);
        }

        public int Delete(long id)
        {
            string sql = $"delete from sys_dictionarytype where id={id}";
            return db.Execute(sql);
        }

        public int Delete(string ids)
        {
            string sql = $"delete from sys_dictionarytype where id in({ids})";
            return db.Execute(sql);
        }

        public int Delete(DictionaryTypeEntity entity)
        {
            string sql = $"delete from sys_dictionarytype where id in={entity.Id}";
            return db.Execute(sql);
        }
        public int Update(DictionaryTypeEntity entity)
        {
            string sql = $"update sys_dictionarytype set parentid={entity.ParentId},name='{entity.Name}',value='{entity.Value}',status={entity.Status},remark='{entity.Remark}',order={entity.Order} where id={entity.Id}";
            return db.Execute(sql);
        }
        #endregion
    }
}
